/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata;

import android.content.Context;
import android.content.Intent;

import app.transittool.model.Stop;

import app.transittool.provider.Tables.Alarms;
import static trs.wmata.TrainLine.BLUE;
import static trs.wmata.TrainLine.GREEN;
import static trs.wmata.TrainLine.ORANGE;
import static trs.wmata.TrainLine.RED;
import static trs.wmata.TrainLine.SILVER;
import static trs.wmata.TrainLine.YELLOW;
public enum TrainStation {
    ADDISON_ROAD_SEAT_PLEASANT("G03", "Addison Road", 38.8867478168, -76.89410791, BLUE, SILVER),
    ANACOSTIA("F06", "Anacostia", 38.8629631168, -76.9953707387, GREEN),
    ARCHIVES_NAVY_MEMORIAL_PENN_QUARTER("F02", "Archives", 38.8936652235, -77.0219143879, GREEN, YELLOW),
    ARLINGTON_CEMETERY("C06", "Arlington Cemetery", 38.8846868585, -77.0628101291, BLUE),
    BALLSTON_MU("K04", "Ballston", 38.8821828738, -77.113168835, ORANGE, SILVER),
    BENNING_ROAD("G01", "Benning Road", 38.890975676, -76.9383648681, BLUE, SILVER),
    BETHESDA("A09", "Bethesda", 38.9843936603, -77.0941291922, RED),
    BRADDOCK_ROAD("C12", "Braddock Road", 38.8141436672, -77.053667574, BLUE, YELLOW),
    BRANCH_AVE("F11", "Branch Avenue", 38.8264463483, -76.9114642177, GREEN),
    BROOKLAND_CUA("B05", "Brookland", 38.9332109913, -76.9945342851, RED),
    CAPITOL_HEIGHTS("G02", "Capitol Heights", 38.8894658568, -76.9118081145, BLUE, SILVER),
    CAPITOL_SOUTH("D05", "Capitol South", 38.8850625009, -77.0051394199, BLUE, ORANGE, SILVER),
    CHEVERLY("D11", "Cheverly", 38.9166318546, -76.916628044, ORANGE),
    CLARENDON("K02", "Clarendon", 38.886704839, -77.0953940983, ORANGE, SILVER),
    CLEVELAND_PARK("A05", "Cleveland Park", 38.9347628908, -77.0580425191, RED),
    COLLEGE_PARK_U_OF_MD("E09", "College Park", 38.9786336339, -76.9281249818, GREEN),
    COLUMBIA_HEIGHTS("E04", "Columbia Heights", 38.9278379675, -77.0325521177, GREEN, YELLOW),
    CONGRESS_HEIGHTS("F07", "Congress Height", 38.8456577028, -76.9885119326, GREEN),
    COURT_HOUSE("K01", "Court House", 38.8901755312, -77.087131231, ORANGE, SILVER),
    CRYSTAL_CITY("C09", "Crystal City", 38.8579043204, -77.0502898097, BLUE, YELLOW),
    DEANWOOD("D10", "Deanwood", 38.9081784965, -76.935256783, ORANGE),
    DUNN_LORING_MERRIFIELD("K07", "Dunn Loring", 38.8836251359, -77.2271606721, ORANGE),
    DUPONT_CIRCLE("A03", "Dupont Circle", 38.9095980575, -77.0434143597, RED),
    EASTERN_MARKET("D06", "Eastern Market", 38.8846222608, -76.9960011267, BLUE, ORANGE, SILVER),
    EAST_FALLS_CHURCH("K05", "E Falls Church", 38.8859531663, -77.1568830199, ORANGE, SILVER),
    EISENHOWER_AVENUE("C14", "Eisenhower Avenue", 38.8004254497, -77.0708743893, YELLOW),
    FARRAGUT_NORTH("A02", "Farragut North", 38.9032019462, -77.0397008272, RED),
    FARRAGUT_WEST("C03", "Farragut West", 38.9013128941, -77.0406954151, BLUE, ORANGE, SILVER),
    FEDERAL_CENTER_SW("D04", "Federal Center SW", 38.8850723551, -77.0158682169, BLUE, ORANGE, SILVER),
    FEDERAL_TRIANGLE("D01", "Federal Triangle", 38.8931808718, -77.0281319984, BLUE, ORANGE, SILVER),
    FOGGY_BOTTOM_GWU("C04", "Foggy Bottom", 38.9006980092, -77.050277739, BLUE, ORANGE, SILVER),
    FOREST_GLEN("B09", "Forest Glen", 39.0149542752, -77.0429165548, RED),
    FORT_TOTTEN("B06", "E06", "Fort Totten", 38.9518467675, -77.0022030768, GREEN, RED, YELLOW),
    FRANCONIA_SPRINGFIELD("J03", "Franconia-Springf'ld", 38.7665218926, -77.1679701804, BLUE),
    FRIENDSHIP_HEIGHTS("A08", "Friendship Heights", 38.9594838736, -77.084995805, RED),
    GALLERY_PL_CHINATOWN("B01", "F01", "Gallery Place", 38.8983168097, -77.0219153904, GREEN, RED, YELLOW),
    GEORGIA_AVE_PETWORTH("E05", "Georgia Avenue", 38.9374346301, -77.023460904, GREEN, YELLOW),
    GLENMONT("B11", "Glenmont", 39.0617837655, -77.0535573593, RED),
    GREENBELT("E10", "Greenbelt", 39.0111458605, -76.9110575731, GREEN),
    GREENSBORO("N03", "Greensboro", 38.921732, -77.234607, SILVER),
    GROSVENOR_STRATHMORE("A11", "Grosvenor", 39.02926895, -77.10384972, RED),
    HUNTINGTON("C15", "Huntington", 38.7939158529, -77.0752057891, YELLOW),
    JUDICIARY_SQUARE("B02", "Judiciary Square", 38.8960903176, -77.0166389566, RED),
    KING_STREET("C13", "King Street", 38.8065861172, -77.0608112085, BLUE, YELLOW),
    LANDOVER("D12", "Landover", 38.9335062344, -76.8911979676, ORANGE),
    LARGO_TOWN_CENTER("G05", "Largo Town Center", 38.9050688072, -76.8420375202, BLUE, SILVER),
    L_ENFANT_PLAZA("D03", "F03", "L'Enfant Plaza", 38.8848377279, -77.021908484, BLUE, GREEN, ORANGE, SILVER, YELLOW),
    MCLEAN("N01", "McLean", 38.924432, -77.210295, SILVER),
    MCPHERSON_SQUARE("C02", "McPherson Square", 38.9013327968, -77.0336341721, BLUE, ORANGE, SILVER),
    MEDICAL_CENTER("A10", "Medical Center", 39.0000564843, -77.0969522905, RED),
    METRO_CENTER("A01", "C01", "Metro Center", 38.8983144732, -77.0280779971, BLUE, ORANGE, RED, SILVER),
    MINNESOTA_AVE("D09", "Minnesota Avenue", 38.899191223, -76.9467477336, ORANGE),
    MORGAN_BOULEVARD("G04", "Morgan Blvd", 38.8938349282, -76.8680747681, BLUE, SILVER),
    MT_VERNON_SQ_7TH_ST_CONVENTION_CENTER("E01", "Mt Vernon Sq", 38.9064368149, -77.0219143803, GREEN, YELLOW),
    NAVY_YARD("F05", "Navy Yard", 38.8764810849, -77.0050856513, GREEN),
    NAYLOR_ROAD("F09", "Naylor Road", 38.8513013835, -76.9562627094, GREEN),
    NEW_CARROLLTON("D13", "New Carrollton", 38.9477848558, -76.8718412865, ORANGE),
    NEW_YORK_AVE_FLORIDA_AVE_GALLAUDET_U("B35", "New York Avenue", 38.9070162121, -77.0030204472, RED),
    PENTAGON("C07", "Pentagon", 38.8694627012, -77.0537156734, BLUE, YELLOW),
    PENTAGON_CITY("C08", "Pentagon City", 38.8618823867, -77.0595389215, BLUE, YELLOW),
    POTOMAC_AVE("D07", "Potomac Avenue", 38.8812632736, -76.9854953196, BLUE, ORANGE, SILVER),
    PRINCE_GEORGE_S_PLAZA("E08", "Prince Georges Plaza", 38.9653854458, -76.9558815078, GREEN),
    RHODE_ISLAND_AVE_BRENTWOOD("B04", "Rhode Island Avenue", 38.9210596891, -76.9959369166, RED),
    ROCKVILLE("A14", "Rockville", 39.0843216075, -77.1461253392, RED),
    RONALD_REAGAN_WASHINGTON_NATIONAL_AIRPORT("C10", "National Arpt", 38.8534163859, -77.0440422943, BLUE, YELLOW),
    ROSSLYN("C05", "Rosslyn", 38.8959790962, -77.0709086853, BLUE, ORANGE, SILVER),
    SHADY_GROVE("A15", "Shady Grove", 39.1199273249, -77.1646273343, RED),
    SHAW_HOWARD_U("E02", "Shaw", 38.9134768711, -77.0219117007, GREEN, YELLOW),
    SILVER_SPRING("B08", "Silver Spring", 38.9939493747, -77.0310178268, RED),
    SMITHSONIAN("D02", "Smithsonian", 38.888018702, -77.0280662342, BLUE, ORANGE, SILVER),
    SOUTHERN_AVENUE("F08", "Southern Ave", 38.8410857803, -76.9750541388, GREEN),
    SPRING_HIll("N04", "Spring Hill", 38.928872, -77.241472, SILVER),
    STADIUM_ARMORY("D08", "Stadium Armory", 38.8867090898, -76.9770889014, BLUE, ORANGE, SILVER),
    SUITLAND("F10", "Suitland", 38.8439645544, -76.9318701589, GREEN),
    TAKOMA("B07", "Takoma", 38.976078531, -77.0181766987, RED),
    TENLEYTOWN_AU("A07", "Tenleytown", 38.9488514351, -77.0795873255, RED),
    TWINBROOK("A13", "Twinbrook", 39.0624676517, -77.1208179517, RED),
    TYSONS_CORNER("N02", "Tysons Corner", 38.920496, -77.222262, SILVER),
    UNION_STATION("B03", "Union Station", 38.8977660392, -77.0074142921, RED),
    U_STREET_AFRICAN_AMER_CIVIL_WAR_MEMORIAL_CARDOZO("E03", "U Street", 38.9170023992, -77.0274958929, GREEN, YELLOW),
    VAN_DORN_STREET("J02", "Van Dorn St", 38.799307672, -77.1291115237, BLUE),
    VAN_NESS_UDC("A06", "Van Ness UDC", 38.9432652883, -77.0629861805, RED),
    VIENNA_FAIRFAX_GMU("K08", "Vienna", 38.8776011238, -77.2726222569, ORANGE),
    VIRGINIA_SQUARE_GMU("K03", "Virginia Square", 38.8833661518, -77.1029772942, ORANGE, SILVER),
    WATERFRONT_SEU("F04", "Waterfront", 38.8764618668, -77.0175052088, GREEN),
    WEST_FALLS_CHURCH_VT_UVA("E07", "W Falls Church", 38.9550401707, -76.9695766751, GREEN),
    WEST_HYATTSVILLE("K06", "West Hyattsville", 38.900780551, -77.1890948225, ORANGE),
    WHEATON("B10", "Wheaton", 39.0375271436, -77.0501070535, RED),
    WHITE_FLINT("A12", "White Flint", 39.0481513573, -77.112829859, RED),
    WIEHLE_RESTON_EAST("N06", "Wiehle-Reston East", 38.94778, -77.34027, SILVER),
    WOODLEY_PARK_ZOO_ADAMS_MORGAN("A04", "Woodley Park Zoo", 38.9250851371, -77.0524180207, RED),
    UNKNOWN("UNKNOWN", "UNKNOWN", -1, -1, TrainLine.UNKNOWN);
    private final String apiCode;
    private final String apiAltCode;
    private final String name;
    private final double lat;
    private final double lon;
    private final TrainLine[] trainLines;

    private TrainStation(final String apiCode, final String name, final double lat, final double lon, final TrainLine... trainLines) {
        this(apiCode, null, name, lat, lon, trainLines);
    }

    private TrainStation(final String apiCode, final String apiAltCode, final String name, final double lat, final double lon, final TrainLine... trainLines) {
        this.apiCode = apiCode;
        this.apiAltCode = apiAltCode;
        this.name = name;
        this.lat = lat;
        this.lon = lon;
        this.trainLines = trainLines;
    }

    public String getCode() {
        return apiCode;
    }

    public String getAltCode() {
        return apiAltCode;
    }

    public String getName() {
        return name;
    }

    public double getLat() {
        return lat;
    }

    public double getLon() {
        return lon;
    }

    public Stop newInstance(final Context context) {
        return new Stop() {
                @Override
                public String getCode() {
                    return TrainStation.this.getCode();
                }

                @Override
                public String getName() {
                    return TrainStation.this.getName();
                }

                @Override
                public double getLat() {
                    return TrainStation.this.getLat();
                }

                @Override
                public double getLon() {
                    return TrainStation.this.getLon();
                }

                @Override
                public String[] getRouteCodes() {
                    final String[] routeCodes = new String[trainLines.length];

                    for (int i = 0; i < routeCodes.length; i++) {
                        routeCodes[i] = trainLines[i].getCode();
                    }

                    return routeCodes;
                }

                @Override
                public String[] getRouteNames() {
                    final String[] routeNames = new String[trainLines.length];

                    for (int i = 0; i < routeNames.length; i++) {
                        routeNames[i] = trainLines[i].newInstance(context).getName();
                    }

                    return routeNames;
                }

                @Override
                public boolean gotoView() {
                    final Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.putExtra(Alarms.AGENCY_ID, TrainAgency.WMATA_TRAIN_AGENCY_ID);
                    intent.putExtra(Alarms.STOP, getCode());
                    intent.setType("application/wmata-nexttrain");

                    context.startActivity(intent);

                    return true;
                }

                @Override
                public boolean gotoViewNearbyStops() {
                    final Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.putExtra(Alarms.AGENCY_ID, TrainAgency.WMATA_TRAIN_AGENCY_ID);
                    intent.putExtra(Alarms.STOP, getCode());
                    intent.setType("application/wmata-nearbytrain");

                    context.startActivity(intent);

                    return true;
                }
            };
    }

    public static TrainStation fromCode(final String code) {
        if (code != null) {
            for (final TrainStation station : TrainStation.values()) {
                if (station.getCode().equals(code)) {
                    return station;
                } else if ((station.getAltCode() != null) && station.getAltCode().equals(code)) {
                    return station;
                }
            }
        }

        throw new IllegalArgumentException("Unrecognized TrainStation code:" + code);
    }
}
